package silo.dungeonbuddy;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferStrategy;
import java.util.Vector;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRootPane;

import java.awt.image.BufferedImage;
//import java.awt.Dimension;
import java.io.IOException;

public class Form extends Canvas
{
	protected BufferStrategy strategy;
	protected JFrame container;
	protected int x;
	protected int y;
	protected int width;
	protected int height;
	protected String title;
	protected String name;
	protected Start game;
	protected Vector textBoxes = new Vector();
	protected TextBox selectedTextBox;
	protected Vector buttons = new Vector();
	protected Vector labels = new Vector();
	protected Vector images = new Vector();
	protected Vector scrollBars = new Vector();
	protected Vector checkBoxes = new Vector();
	protected boolean scrolling = false;
	protected ScrollBar selectedScrollBar=null;
	protected FlatImage dragTerrain;
	//KEYS  v v
	protected boolean kUp = false;
	protected boolean kDown = false;
	protected boolean kLeft = false;
	protected boolean kRight = false;
	protected boolean kA = false;
	protected boolean kB = false;
	protected boolean kC = false;
	protected boolean kD = false;
	protected boolean kE = false;
	protected boolean kF = false;
	protected boolean kG = false;
	protected boolean kH = false;
	protected boolean kI = false;
	protected boolean kJ = false;
	protected boolean kK = false;
	protected boolean kL = false;
	protected boolean kM = false;
	protected boolean kN = false;
	protected boolean kO = false;
	protected boolean kP = false;
	protected boolean kQ = false;
	protected boolean kR = false;
	protected boolean kS = false;
	protected boolean kT = false;
	protected boolean kU = false;
	protected boolean kV = false;
	protected boolean kW = false;
	protected boolean kX = false;
	protected boolean kY = false;
	protected boolean kZ = false;
	protected boolean k0 = false;
	protected boolean k1 = false;
	protected boolean k2 = false;
	protected boolean k3 = false;
	protected boolean k4 = false;
	protected boolean k5 = false;
	protected boolean k6 = false;
	protected boolean k7 = false;
	protected boolean k8 = false;
	protected boolean k9 = false;
	protected boolean kBackspace = false;
	protected boolean kPeriod = false;
	protected boolean kMinus = false;
	protected boolean kSpace = false;
	protected boolean kEnter = false;
	protected boolean kShift = false;
	protected boolean kControl = false;
	//KEYS  ^ ^
	//MOUSE v v
	protected int mouseX;
	protected int mouseY;
	protected boolean mouseLeft=false;
	protected boolean mouseRight=false;
	protected boolean doubleClick;
	protected long clickTime;
	protected int clickedPosX;
	protected int clickedPosY;
	//MOUSE ^ ^

	public Form(String eName, Start eGame, int eX, int eY, int eWidth, int eHeight, String eTitle)
	{
		name=eName;
		x=eX;
		y=eY;
		width=eWidth;
		height=eHeight;
		game=eGame;
		title=eTitle;
		
		// create a frame to contain the game
		container = new JFrame(title);

		// get hold the content of the frame and set up the resolution of the game
		JPanel panel = (JPanel)container.getContentPane();
		if(title.contains("ERROR"))
		{
			//Eventually, I'll make a separate class for ERROR dialogues, using JDialogue, instead of JFrame
			container.setUndecorated(true);
			container.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
			container.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
			container.setResizable(false);

		}
		panel.setPreferredSize(new Dimension(width, height));
		panel.setLayout(null);

		// setup canvas size and put it into the content of the frame
		setBounds(0, 0, width, height);
		container.setLocation(x, y);
		panel.add(this);

		// Tell AWT not to repaint the canvas since this will be done
		// in accelerated mode
		setIgnoreRepaint(true);

		// make the window visible 
		container.pack();
		container.setResizable(false);
		container.setVisible(true);
		BufferedImage image = null;
		try
		{
			image = ImageIO.read(container.getClass().getResource("/sprites/icon.jpg"));
		}catch (IOException e)
		{
			e.printStackTrace();
		}
		container.setIconImage(image);
		//container.setIconImage(new ImageIcon("").getImage());


		// add a listener to respond to the user closing the window. If they
		// do - exit the game
		container.addWindowListener(new WindowAdapter()
		{
			public void windowClosing(WindowEvent e)
			{
				closingEvent();
				game.getFormList().remove(this);
				//container.dispose();
			}
		});
		container.addWindowFocusListener(new WindowAdapter()
		{
			public void windowLostFocus(WindowEvent e) 
			{
				if(title.contains("ERROR"))
				{
					requestFocus();
				}
			}
		});
		
		// add a key input system (defined below) to canvas
		// to respond to key presses
		addKeyListener(new KeyInputHandler());
		//also mouse input class:
		addMouseListener(new MouseInputHandler());
		
		// GET FOCUS
		requestFocus();

		// CREATE BUFFER STRATEGY SO AWT WILL HANDLE ACCELERATED
		// GRAPHICS.
		createBufferStrategy(2);
		strategy = getBufferStrategy();
		
	}//Constructor
	
	public void draw()
	{
		// GET CONTEXT FOR ACCELERATED GRAPHICS
		Graphics2D g = (Graphics2D) strategy.getDrawGraphics();
		g.setColor(Color.gray);
		g.fillRect(0,0,width,height);
		if(textBoxes.size()>0)
		{
			for(int i=0;i<textBoxes.size();i++)
			{
				TextBox t = (TextBox)textBoxes.get(i);
				t.draw(g);
			}//next i
		}//if(textBoxes.size()>0)
		if(buttons.size()>0)
		{
			for(int i=0;i<buttons.size();i++)
			{
				Button b = (Button)buttons.get(i);
				b.draw(g);
			}//next i
		}//if(buttons.size()>0)
		if(checkBoxes.size()>0)
		{
			for(int i=0; i<checkBoxes.size(); i++)
			{
				CheckBox c = (CheckBox)checkBoxes.get(i);
				c.draw(g);
			}//next i
		}//if(checkBoxes.size()>0)
		if(labels.size()>0)
		{
			for(int i=0;i<labels.size();i++)
			{
				Label l = (Label)labels.get(i);
				l.draw(g);
			}//next i
		}//if(labels.size()>0)
		if(images.size()>0)
		{
			for(int i=0;i<images.size();i++)
			{
				FlatImage f = (FlatImage)images.get(i);
				f.draw(g);
			}//next i
		}//if(images.size()>0)
		if(scrollBars.size()>0)
		{
			for(int i=0;i<scrollBars.size();i++)
			{
				ScrollBar s = (ScrollBar)scrollBars.get(i);
				s.draw(g);
			}//next i
		}//if(scrollBars.size()>0)
		
		//DISPOSE OF GRAPHICS OBJECT AND FLIP BUFFER
		g.dispose();
		strategy.show();
	}//draw
	
	public void function(Graphics2D g)
	{
		if(isFocusOwner())
		{
			// For now, focus is not an issue, since the last window will have focus
			// until it is closed. (see this comment in the "function" sub in the ToolBox class)
			checkScrollBars();
			checkButtons(g);
			checkTextBoxes();
			checkKeys();
			checkMouse();
		}
	}//function
	
	public void checkKeys()
	{
		// I WOULD LIKE TO MOVE THIS SUBROUTINE TO A DIFFERENT
		// CLASS FOR ORGANIZATION PURPOSES, BUT I HAVEN'T DONE
		// IT YET...
		if(this==game.getToolBox())
		{
			if(kUp)
			{
				game.getToolBox().setOffset(game.getToolBox().getOffsetValue()-1);
				kUp=false;
			}
			if(kDown)
			{
				game.getToolBox().setOffset(game.getToolBox().getOffsetValue()+1);
				kDown=false;
			}
		}
		if(kSpace)
		{
			//player.fire();
			if(selectedTextBox!=null)
			{
				selectedTextBox.setText(selectedTextBox.getText() + " ");
			}
			kSpace = false;
		}
		if (kP==true)
		{
			if(selectedTextBox!=null)
			{
				if(kShift)
				{
					selectedTextBox.setText(selectedTextBox.getText() + "P");
					kP=false;
				}else
				{
					selectedTextBox.setText(selectedTextBox.getText() + "p");
					kP=false;
				}
			}
			kP=false;
		}
		if (kQ==true)
		{
			if(selectedTextBox!=null)
			{
				if(kShift)
				{
					selectedTextBox.setText(selectedTextBox.getText() + "Q");
				}else
				{
					selectedTextBox.setText(selectedTextBox.getText() + "q");
				}
				kQ=false;
			}
		}
		if (kW==true)
		{
			if(selectedTextBox!=null)
			{
				if(kShift)
				{
					selectedTextBox.setText(selectedTextBox.getText() + "W");
				}else
				{
					selectedTextBox.setText(selectedTextBox.getText() + "w");
				}
				kW=false;
			}
		}
		if (kE==true)
		{
			if(selectedTextBox!=null)
			{
				if(kShift)
				{
					selectedTextBox.setText(selectedTextBox.getText() + "E");
				}else
				{
					selectedTextBox.setText(selectedTextBox.getText() + "e");
				}
				kE=false;
			}
		}
		if (kA==true)
		{
			if(selectedTextBox!=null)
			{
				if(kShift)
				{
					selectedTextBox.setText(selectedTextBox.getText() + "A");
				}else
				{
					selectedTextBox.setText(selectedTextBox.getText() + "a");
				}
				kA=false;
			}
		}
		if (kS==true)
		{
			if(selectedTextBox!=null)
			{
				if(kShift)
				{
					selectedTextBox.setText(selectedTextBox.getText() + "S");
				}else
				{
					selectedTextBox.setText(selectedTextBox.getText() + "s");
				}
				kS=false;
			}
		}
		if (kD==true)
		{
			if(selectedTextBox!=null)
			{
				if(kShift)
				{
					selectedTextBox.setText(selectedTextBox.getText() + "D");
				}else
				{
					selectedTextBox.setText(selectedTextBox.getText() + "d");
				}
				kD=false;
			}
		}
		if (kZ==true)
		{
			if(selectedTextBox!=null)
			{
				if(kShift)
				{
					selectedTextBox.setText(selectedTextBox.getText() + "Z");
				}else
				{
					selectedTextBox.setText(selectedTextBox.getText() + "z");
				}
				kZ=false;
			}
		}
		if (kX==true)
		{
			if(selectedTextBox!=null)
			{
				if(kShift)
				{
					selectedTextBox.setText(selectedTextBox.getText() + "X");
				}else
				{
					selectedTextBox.setText(selectedTextBox.getText() + "x");
				}
				kX=false;
			}
		}
		if (kC==true)
		{
			if(selectedTextBox!=null)
			{
				if(kShift)
				{
					selectedTextBox.setText(selectedTextBox.getText() + "C");
				}else
				{
					selectedTextBox.setText(selectedTextBox.getText() + "c");
				}
				kC=false;
			}
		}
		if(kB)
		{
			if(selectedTextBox!=null)
			{
				if(kShift)
				{
					selectedTextBox.setText(selectedTextBox.getText() + "B");
				}else
				{
					selectedTextBox.setText(selectedTextBox.getText() + "b");
				}
				kB=false;
			}
		}
		if(kF)
		{
			if(selectedTextBox!=null)
			{
				if(kShift)
				{
					selectedTextBox.setText(selectedTextBox.getText() + "F");
				}else
				{
					selectedTextBox.setText(selectedTextBox.getText() + "f");
				}
				kF=false;
			}
		}
		if(kG)
		{
			if(selectedTextBox!=null)
			{
				if(kShift)
				{
					selectedTextBox.setText(selectedTextBox.getText() + "G");
				}else
				{
					selectedTextBox.setText(selectedTextBox.getText() + "g");
				}
				kG=false;
			}
		}
		if(kH)
		{
			if(selectedTextBox!=null)
			{
				if(kShift)
				{
					selectedTextBox.setText(selectedTextBox.getText() + "H");
				}else
				{
					selectedTextBox.setText(selectedTextBox.getText() + "h");
				}
				kH=false;
			}
		}
		if(kI)
		{
			if(selectedTextBox!=null)
			{
				if(kShift)
				{
					selectedTextBox.setText(selectedTextBox.getText() + "I");
				}else
				{
					selectedTextBox.setText(selectedTextBox.getText() + "i");
				}
				kI=false;
			}
		}
		if(kJ)
		{
			if(selectedTextBox!=null)
			{
				if(kShift)
				{
					selectedTextBox.setText(selectedTextBox.getText() + "J");
				}else
				{
					selectedTextBox.setText(selectedTextBox.getText() + "j");
				}
				kJ=false;
			}
		}
		if(kK)
		{
			if(selectedTextBox!=null)
			{
				if(kShift)
				{
					selectedTextBox.setText(selectedTextBox.getText() + "K");
				}else
				{
					selectedTextBox.setText(selectedTextBox.getText() + "k");
				}
				kK=false;
			}
		}
		if(kL)
		{
			if(selectedTextBox!=null)
			{
				if(kShift)
				{
					selectedTextBox.setText(selectedTextBox.getText() + "L");
				}else
				{
					selectedTextBox.setText(selectedTextBox.getText() + "l");
				}
				kL=false;
			}
		}
		if(kM)
		{
			if(selectedTextBox!=null)
			{
				if(kShift)
				{
					selectedTextBox.setText(selectedTextBox.getText() + "M");
				}else
				{
					selectedTextBox.setText(selectedTextBox.getText() + "m");
				}
				kM=false;
			}
		}
		if(kN)
		{
			if(selectedTextBox!=null)
			{
				if(kShift)
				{
					selectedTextBox.setText(selectedTextBox.getText() + "N");
				}else
				{
					selectedTextBox.setText(selectedTextBox.getText() + "n");
				}
				kN=false;
			}
		}
		if(kO)
		{
			if(selectedTextBox!=null)
			{
				if(kShift)
				{
					selectedTextBox.setText(selectedTextBox.getText() + "O");
				}else
				{
					selectedTextBox.setText(selectedTextBox.getText() + "o");
				}
				kO=false;
			}
		}
		if(kR)
		{
			if(selectedTextBox!=null)
			{
				if(kShift)
				{
					selectedTextBox.setText(selectedTextBox.getText() + "R");
				}else
				{
					selectedTextBox.setText(selectedTextBox.getText() + "r");
				}
				kR=false;
			}
		}
		if(kT)
		{
			if(selectedTextBox!=null)
			{
				if(kShift)
				{
					selectedTextBox.setText(selectedTextBox.getText() + "T");
				}else
				{
					selectedTextBox.setText(selectedTextBox.getText() + "t");
				}
				kT=false;
			}
		}
		if(kU)
		{
			if(selectedTextBox!=null)
			{
				if(kShift)
				{
					selectedTextBox.setText(selectedTextBox.getText() + "U");
				}else
				{
					selectedTextBox.setText(selectedTextBox.getText() + "u");
				}
				kU=false;
			}
		}
		if(kV)
		{
			if(selectedTextBox!=null)
			{
				if(kShift)
				{
					selectedTextBox.setText(selectedTextBox.getText() + "V");
				}else
				{
					selectedTextBox.setText(selectedTextBox.getText() + "v");
				}
				kV=false;
			}
		}
		if(kY)
		{
			if(selectedTextBox!=null)
			{
				if(kShift)
				{
					selectedTextBox.setText(selectedTextBox.getText() + "Y");
				}else
				{
					selectedTextBox.setText(selectedTextBox.getText() + "y");
				}
				kY=false;
			}
		}
		if (k0==true)
		{
			if(selectedTextBox!=null)
			{
				if(kShift)
				{
					selectedTextBox.setText(selectedTextBox.getText() + ")");
				}else
				{
					selectedTextBox.setText(selectedTextBox.getText() + "0");
				}
				k0=false;
			}
		}
		if (k1==true)
		{
			if(selectedTextBox!=null)
			{
				selectedTextBox.setText(selectedTextBox.getText() + "1");
				k1=false;
			}
		}
		if (k2==true)
		{
			if(selectedTextBox!=null)
			{
				selectedTextBox.setText(selectedTextBox.getText() + "2");
				k2=false;
			}
		}
		if (k3==true)
		{
			if(selectedTextBox!=null)
			{
				selectedTextBox.setText(selectedTextBox.getText() + "3");
				k3=false;
			}
		}
		if (k4==true)
		{
			if(selectedTextBox!=null)
			{
				selectedTextBox.setText(selectedTextBox.getText() + "4");
				k4=false;
			}
		}
		if (k5==true)
		{
			if(selectedTextBox!=null)
			{
				selectedTextBox.setText(selectedTextBox.getText() + "5");
				k5=false;
			}
		}
		if (k6==true)
		{
			if(selectedTextBox!=null)
			{
				selectedTextBox.setText(selectedTextBox.getText() + "6");
				k6=false;
			}
		}
		if (k7==true)
		{
			if(selectedTextBox!=null)
			{
				selectedTextBox.setText(selectedTextBox.getText() + "7");
				k7=false;
			}
		}
		if (k8==true)
		{
			if(selectedTextBox!=null)
			{
				selectedTextBox.setText(selectedTextBox.getText() + "8");
				k8=false;
			}
		}
		if (k9==true)
		{
			if(selectedTextBox!=null)
			{
				if(kShift)
				{
					selectedTextBox.setText(selectedTextBox.getText() + "(");
				}else
				{
					selectedTextBox.setText(selectedTextBox.getText() + "9");
				}
				k9=false;
			}
		}
		if (kPeriod==true)
		{
			if(selectedTextBox!=null)
			{
				selectedTextBox.setText(selectedTextBox.getText() + ".");
				kPeriod=false;
			}
		}
		if (kBackspace==true)
		{
			if(selectedTextBox!=null)
			{
				if((selectedTextBox.getText().length()-1)>=0)
				{
					selectedTextBox.setText(selectedTextBox.getText().substring(0,selectedTextBox.getText().length()-1));
					kBackspace=false;
				}
			}
		}
		if (kMinus==true)
		{
			if(selectedTextBox!=null)
			{
				selectedTextBox.setText(selectedTextBox.getText() + "-");
				kMinus=false;
			}
		}
	}//checkKeys
	
	protected void checkMouse()
	{
		if(doubleClick)
		{
			doubleClick=false;
		}else if((game.getClient())&&(mouseLeft)&&(checkBoxes.size()>0))//if(doubleClick)
		{
			for(int i=0; i<checkBoxes.size(); i++)
			{
				CheckBox cK = (CheckBox)checkBoxes.get(i);
				if((mouseX>cK.getX())&&(mouseX<cK.getX()+12)&&(mouseY>cK.getY())&&(mouseY<cK.getY()+12))
				{
					if(cK.getSelected())
					{
						cK.setSelected(false);
					}else
					{
						cK.setSelected(true);
					}
					i=checkBoxes.size();
				}//if((mouseX>cK.getX())&&(mouseX<cK.getX()+12)&&(mouseY>cK.getY())&&(mouseY<cK.getY()+12))
			}//next i
			mouseLeft=false;
		}//else if((!game.getClient())&&(mouseLeft)&&(checkBoxes.size()>0)) -- if(doubleClick)
	}//protected void checkMouse()
	
	protected void checkButtons(Graphics2D g)
	{
		for(int i=0;i<buttons.size();i++)
		{
			Button b = (Button) buttons.get(i);
			if((mouseLeft)&&(b.getEnabled())&&(mouseX>b.getX())&&(mouseX<(b.getX()+b.getWidth()))&&(mouseY>b.getY())&&(mouseY<(b.getY()+b.getHeight())))
			{
				b.setPressed(true);
			}else if((b.getPressed())&&(!mouseLeft)&&(mouseX>b.getX())&&(mouseX<(b.getX()+b.getWidth()))&&(mouseY>b.getY())&&(mouseY<(b.getY()+b.getHeight())))
			{
				b.setPressed(false);
				b.formAction(this, g);
			}else if((b.getPressed())&&(!mouseLeft))
			{
				b.setPressed(false);
			}
		}//next i
	}//checkButtons
	
	protected void checkTextBoxes()
	{
		for(int i=0;i<textBoxes.size();i++)
		{
			TextBox t = (TextBox) textBoxes.get(i);
			if((mouseLeft)&&(mouseX>t.getX())&&(mouseX<(t.getX()+t.getWidth()))&&(mouseY>t.getY())&&(mouseY<(t.getY()+t.getHeight())))
			{
				if(selectedTextBox!=null)
				{
					selectedTextBox.setSelected(false);
				}
				t.setSelected(true);
				selectedTextBox=t;
				mouseLeft=false;
				break;
			}
		}//next i
	}//checkTextBoxes
	
	protected void checkScrollBars()
	{
		if(scrollBars.size()>0)
		{
			for(int i=0;i<scrollBars.size();i++)
			{
				ScrollBar s = (ScrollBar)scrollBars.get(i);
				s.function(this);
			}//next i
		}//if(scrollBars.size()>0)
	}
	
	public void close()
	{
		closingEvent();
		game.getFormList().remove(this);
		container.dispose();
	}
	public void closingEvent()
	{
		
	}
	
	//GETTERS
	public Vector getTextBoxes()
	{
		return textBoxes;
	}
	public Vector getButtons()
	{
		return buttons;
	}
	public Vector getLabels()
	{
		return labels;
	}
	public Vector getImages()
	{
		return images;
	}
	public int getMouseX()
	{
		return mouseX;
	}
	public int getMouseY()
	{
		return mouseY;
	}
	public boolean getScrolling()
	{
		return scrolling;
	}
	public ScrollBar getSelectedScrollBar()
	{
		return selectedScrollBar;
	}
	public boolean getMouseLeft()
	{
		return mouseLeft;
	}
	public Vector getCheckBoxes()
	{
		return checkBoxes;
	}
	
	//SETTERS
	public void setTextBoxes(Vector eTs)
	{
		textBoxes=eTs;
	}
	public void setButtons(Vector eBs)
	{
		buttons=eBs;
	}
	public void setLabels(Vector eLs)
	{
		labels=eLs;
	}
	public void setImages(Vector eIs)
	{
		images=eIs;
	}
	public void setScrolling(boolean eScrolling)
	{
		scrolling=eScrolling;
	}
	public void setSelectedScrollBar(ScrollBar eSelectedScrollBar)
	{
		selectedScrollBar=eSelectedScrollBar;
	}
	
	//KEY LISTENER
	protected class KeyInputHandler extends KeyAdapter
	{
		public void keyPressed(KeyEvent e)
		{
			if (e.getKeyCode() == KeyEvent.VK_BACK_SPACE)
			{
				kBackspace = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_MINUS)
			{
				kMinus = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_1)
			{
				k1 = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_2)
			{
				k2 = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_3)
			{
				k3 = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_4)
			{
				k4 = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_5)
			{
				k5 = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_6)
			{
				k6 = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_7)
			{
				k7 = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_8)
			{
				k8 = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_9)
			{
				k9 = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_0)
			{
				k0 = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_PERIOD)
			{
				kPeriod = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_A)
			{
				kA = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_B)
			{
				kB = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_C)
			{
				kC = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_D)
			{
				kD = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_E)
			{
				kE = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_F)
			{
				kF = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_G)
			{
				kG = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_H)
			{
				kH = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_I)
			{
				kI = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_J)
			{
				kJ = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_K)
			{
				kK = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_L)
			{
				kL = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_M)
			{
				kM = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_N)
			{
				kN = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_O)
			{
				kO = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_P)
			{
				kP = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_Q)
			{
				kQ = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_R)
			{
				kR = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_S)
			{
				kS = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_T)
			{
				kT = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_U)
			{
				kU = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_V)
			{
				kV = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_W)
			{
				kW = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_X)
			{
				kX = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_Y)
			{
				kY = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_Z)
			{
				kZ = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_LEFT)
			{
				kLeft = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_RIGHT)
			{
				kRight = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_UP)
			{
				kUp = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_DOWN)
			{
				kDown = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_SPACE)
			{
				kSpace = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_ENTER)
			{
				kEnter = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_SHIFT)
			{
				kShift = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_CONTROL)
			{
				kControl = true;
			}
		} 
		
		public void keyReleased(KeyEvent e)
		{
			if (e.getKeyCode() == KeyEvent.VK_BACK_SPACE)
			{
				kBackspace = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_MINUS)
			{
				kMinus = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_1)
			{
				k1 = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_2)
			{
				k2 = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_3)
			{
				k3 = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_4)
			{
				k4 = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_5)
			{
				k5 = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_6)
			{
				k6 = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_7)
			{
				k7 = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_8)
			{
				k8 = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_9)
			{
				k9 = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_0)
			{
				k0 = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_PERIOD)
			{
				kPeriod = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_A)
			{
				kA = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_B)
			{
				kB = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_C)
			{
				kC = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_D)
			{
				kD = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_E)
			{
				kE = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_F)
			{
				kF = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_G)
			{
				kG = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_H)
			{
				kH = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_I)
			{
				kI = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_J)
			{
				kJ = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_K)
			{
				kK = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_L)
			{
				kL = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_M)
			{
				kM = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_N)
			{
				kN = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_O)
			{
				kO = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_P)
			{
				kP = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_Q)
			{
				kQ = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_R)
			{
				kR = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_S)
			{
				kS = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_T)
			{
				kT = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_U)
			{
				kU = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_V)
			{
				kV = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_W)
			{
				kW = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_X)
			{
				kX = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_Y)
			{
				kY = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_Z)
			{
				kZ = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_LEFT)
			{
				kLeft = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_RIGHT)
			{
				kRight = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_UP)
			{
				kUp = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_DOWN)
			{
				kDown = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_SPACE)
			{
				kSpace = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_ENTER)
			{
				kEnter = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_SHIFT)
			{
				kShift = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_CONTROL)
			{
				kControl = false;
			}
		}

		public void keyTyped(KeyEvent e)
		{
			if (e.getKeyChar() == 27)
			{
				//System.exit(0);
				selectedTextBox=null;
				for(int i=0;i<textBoxes.size();i++)
				{
					TextBox t = (TextBox) textBoxes.get(i);
					t.setSelected(false);
				}//next i
			}
		}
	}//class KeyInputHandler
	
	
	//MOUSE LISTENER
	protected class MouseInputHandler implements MouseListener, MouseMotionListener
	{

		public void mouseClicked(MouseEvent e)
		{
			// Not used
		}

		public void mouseEntered(MouseEvent e)
		{
			// Not used
		}

		public void mouseExited(MouseEvent e)
		{
			// Not used
		}

		public void mousePressed(MouseEvent e)
		{
			if((e.getModifiers() & e.BUTTON3_MASK)== e.BUTTON3_MASK)
			{
				//This is the right-click event
				mouseRight = true;
				mouseX=e.getX();
				mouseY=e.getY();
			}else
			{
				//This is the left-click event
				mouseLeft = true;
				mouseX=e.getX();
				mouseY=e.getY();
				clickedPosX=mouseX;
				clickedPosY=mouseY;//I could probably take out these variables since I'm only
				   				   //updating mouse position on a click, but I put them in when I
				   				   //was thinking updating mouse position whenever the mouse is
				   				   //moved whenever a form besides the main map form is focused
				   				   //This was implemented, however, only when mouse is dragged in
				   				   //the toolbox form.
				if((System.currentTimeMillis()-clickTime)<400)
				{
					doubleClick=true;
				}
				clickTime=System.currentTimeMillis();
			}
		}//mousePressed

		public void mouseReleased(MouseEvent e)
		{
			if((e.getModifiers() & e.BUTTON3_MASK)== e.BUTTON3_MASK)
			{
				//This is the right-click event
				mouseRight = false;
				mouseX=e.getX();
				mouseY=e.getY();
			}else
			{
				//This is the left-click event
				mouseLeft = false;
				mouseX=e.getX();
				mouseY=e.getY();
			}
		}//mouseReleased

		public void mouseDragged(MouseEvent e)
		{
			//NOT USED
		}//mouseDragged

		public void mouseMoved(MouseEvent e)
		{
			//NOT USED
		}//mouseMoved
		
	}//class MouseInputHandler
}//class Form